﻿using Model.BusinessModel.OutPut;
using Model.BusinessModel.OutPut.sys;
using Utility;

namespace CoreDomain.BusinessCore
{
    public static class TreeCore
    {
        /// <summary>
        /// 转换成树形结构
        /// </summary>
        /// <param name="convertDataList">待转换的数据</param>
        /// <returns></returns>
        public static List<TreeOutPut> ConvertTreeData(this List<TreeOutPut> convertDataList)
        {
            List<TreeOutPut> treeListDto = [];

            var list = convertDataList.Where(f => string.IsNullOrWhiteSpace(f.pKey) || f.pKey == "0").ToList();
            if (list.Count <= 0)
                ExceptionHelper.ThrowBusinessException("需要转换成树形结构的数据,未找到根节点!");

            //递归所有上级数据
            foreach (var data in convertDataList.Where(f => string.IsNullOrWhiteSpace(f.pKey) || f.pKey == "0"))
            {
                var childrenList = GetChildrenData(convertDataList, data.key).ToList();
                data.children = childrenList.Count == 0 ? null : childrenList;
                data.disabled = true;
                treeListDto.Add(data);
            }
            return treeListDto;
        }

        /// <summary>
        /// 实现递归
        /// </summary>
        /// <param name="moduleOutput">数据集合</param>
        /// <param name="key">主键</param>
        /// <returns></returns>
        private static List<TreeOutPut> GetChildrenData(List<TreeOutPut> moduleOutput, string? key)
        {
            List<TreeOutPut> sysShowTempMenus = [];
            //得到子数据
            var info = moduleOutput.Where(w => w.pKey.ToLower() == key.ToLower()).ToList();
            //循环
            foreach (var treeInfo in info)
            {
                treeInfo.isLeaf = true;
                var childrenList = GetChildrenData(moduleOutput, treeInfo.key);
                //把子数据放到Children集合里
                treeInfo.children = childrenList.Count == 0 ? null : childrenList;
                //添加父级数据
                sysShowTempMenus.Add(treeInfo);
            }
            return sysShowTempMenus;
        }


        /// <summary>
        /// 转换成树形结构
        /// </summary>
        /// <param name="convertDataList">待转换的数据</param>
        /// <returns></returns>
        public static List<TreeExtendOutPut<SysButtonDataOutPut>> ConvertTreeData(this List<TreeExtendOutPut<SysButtonDataOutPut>> convertDataList)
        {
            List<TreeExtendOutPut<SysButtonDataOutPut>> treeListDto = [];

            var list = convertDataList.Where(f => string.IsNullOrWhiteSpace(f.pKey) || f.pKey == "0").ToList();
            if (list.Count <= 0)
                ExceptionHelper.ThrowBusinessException("需要转换成树形结构的数据,未找到根节点!");

            //递归所有上级数据
            foreach (var data in convertDataList.Where(f => string.IsNullOrWhiteSpace(f.pKey) || f.pKey == "0"))
            {
                var childrenList = GetChildrenData(convertDataList, data.key).ToList();
                data.children = childrenList.Count == 0 ? null : childrenList;
                data.disabled = true;
                treeListDto.Add(data);
            }
            return treeListDto;
        }

        /// <summary>
        /// 实现递归
        /// </summary>
        /// <param name="moduleOutput">数据集合</param>
        /// <param name="key">主键</param>
        /// <returns></returns>
        private static List<TreeExtendOutPut<SysButtonDataOutPut>> GetChildrenData(List<TreeExtendOutPut<SysButtonDataOutPut>> moduleOutput, string? key)
        {
            List<TreeExtendOutPut<SysButtonDataOutPut>> sysShowTempMenus = [];
            //得到子数据
            var info = moduleOutput.Where(w => w.pKey.ToLower() == key.ToLower()).ToList();
            //循环
            foreach (var treeInfo in info)
            {
                treeInfo.isLeaf = true;
                var childrenList = GetChildrenData(moduleOutput, treeInfo.key);
                //把子数据放到Children集合里
                treeInfo.children = childrenList.Count == 0 ? null : childrenList;
                //添加父级数据
                sysShowTempMenus.Add(treeInfo);
            }
            return sysShowTempMenus;
        }
    }
}
